<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!-- 会把dom缓存起来进行复用 -->
        <keep-alive>
            <div :is="com"></div>
        </keep-alive>
        <button @click="changecom">点击</button>
    </div>
    <script>
        let ComA = {
            template:"<div><h1>我是coma组件</h1><input type='text'/></div>",
            activated:function(){
                console.log("aaa激活的时候触发");
            },
            deactivated:function(){
                console.log("aaaa keep-alive组件被停用");
            }
        }
        let ComB = {
            template:"<div><h2>我是comb组件</h2><input type='text'/></div>",
            activated:function(){
                console.log("bbb激活的时候触发");
            },
            deactivated:function(){
                console.log("bbbkeep-alive组件被停用");
            }

        }

        new Vue({
            el:"#app",
            components:{
                ComA,
                ComB
            },
            data:{
                com:"com-a"
            },
            methods:{
                changecom(){
                    if(this.com=="com-a"){
                        this.com = "com-b";
                    }else{
                        this.com = "com-a";
                    }
                }
            }
        })



    </script>
</body>
</html>